package com.lagou.spring.springboot;

import org.springframework.web.WebApplicationInitializer;

import javax.servlet.ServletContainerInitializer;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.HandlesTypes;
import java.lang.reflect.Modifier;
import java.util.Set;

/**
 * @author handong
 */
@HandlesTypes(WebApplicationInitializer.class)
public class MySpringServletContainerInitializer implements ServletContainerInitializer {

    @Override
    public void onStartup(Set<Class<?>> set, ServletContext servletContext) throws ServletException {
        System.out.println(" 模拟 spring 初始化");
        for (Class<?> clazz : set ) {
            if (!clazz.isInterface() && !Modifier.isAbstract(clazz.getModifiers()) && WebApplicationInitializer.class.isAssignableFrom(clazz)){
                try {
                    ((WebApplicationInitializer) clazz.newInstance()).onStartup(servletContext);
                }catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InstantiationException e) {
                    e.printStackTrace();
                }
            }
        }


    }


}
